#!/usr/bin/python
#-*-coding: utf-8 -*-

__all__ = [u'HandlerPart']

########## DO NOT CHANGE THESE LINES ###########################################
import sys
from pylot.core import (ENCODING, Path)
try : __file__
except NameError : co = sys._getframe(0).f_code ; __file__ = co.co_filename
ufile = __file__.decode(ENCODING)
if ufile.strip() : PLUGIN_PATH = Path(ufile).abspath().parent
else : PLUGIN_PATH = Path(u'.').abspath()
########## DO NOT CHANGE THESE LINES ###########################################

from pylot.core import (check_type, ENCODING, Path)
from pylot.interfaces import (HandlerInterface)

from operators import divide_integers

class HandlerPart (HandlerInterface):
  def __init__ (self, plugin_path=None, plugin_id=None):
    if plugin_path is None : plugin_path = PLUGIN_PATH
    HandlerInterface.__init__(self, plugin_path, plugin_id)
    self.addOptions(u'conf.csv', main=True)

  def _setprecision (self, value):
    assert(0 <= value <= 20)
    self.opt[u'computation'][u'precision'] = value

  precision = property(
    fget=lambda self:self.opt[u'computation'](u'precision'),
    fset=_setprecision)

  def writeDivide(self, a, b):
    res = self.divide(a, b)
    self.io.write(self.tr_(u'result of %d/%d: %s\n') % (a, b, res))

  def divide (self, a, b):
    u"""
    Computes a/b

    :param a: an integer
    :param b: an integer different from 0
    :return: a/b
    :rtype: float
    """
    return round(divide_integers(a, b, self.tr_), self.precision)

  def _readSettings(self, settings, common):
    self.precision = settings.getValue(u'sample/precision', self.precision).value

  def _writeSettings(self, settings, common):
    settings.setValue(u'sample/precision', self.precision)

  def _translate(self):
    self.alias = self.tr_(u'Sample')
    self.tooltip = self.tr_(u'A Sample Plugin')

if __name__ == '__main__':
  u"""
  Example of script
  """
  handler = HandlerPart()
  handler.init()
  handler.precision = 5
  handler.writeDivide(1, 3)
